# Copyright (c) 2018, Lawrence Livermore National Security, LLC. Produced at the
# Lawrence Livermore National Laboratory. LLNL-CODE-745557. All Rights reserved.
# See file COPYRIGHT for details.
#
# This file is part of the ParElag library. For more information and source code
# availability see http://github.com/LLNL/parelag.
#
# ParElag is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License (as published by the Free
# Software Foundation) version 2.1 dated February 1999.

include(CMakeFindDependencyMacro)

# Get the version
include(${CMAKE_CURRENT_LIST_DIR}/ParELAGConfigVersion.cmake)
set(ParELAG_VERSION ${PACKAGE_VERSION})

@PACKAGE_INIT@

# A few of the parameters
set(ParELAG_ENABLE_OPENMP @ParELAG_ENABLE_OPENMP@)
set(ParELAG_ENABLE_PARMETIS @ParELAG_ENABLE_PARMETIS@)
set(ParELAG_ENABLE_SUPERLU @ParELAG_ENABLE_SUPERLU@)
set(ParELAG_ENABLE_SUPERLUDIST @ParELAG_ENABLE_SUPERLUDIST@)
set(ParELAG_ENABLE_STRUMPACK @ParELAG_ENABLE_STRUMPACK@)
set(ParELAG_ENABLE_PETSC @ParELAG_ENABLE_PETSC@)
set(ParELAG_USE_MARS @ParELAG_USE_MARS@)

# Compilers and flags and the like
set(ParELAG_C_COMPILER @CMAKE_C_COMPILER@)
set(ParELAG_CXX_COMPILER @CMAKE_CXX_COMPILER@)
set(ParELAG_MPI_C_COMPILER @MPI_C_COMPILER@)
set(ParELAG_MPI_CXX_COMPILER @MPI_CXX_COMPILER@)

#
# DEPENDENCIES:
#
set_and_check(ParELAG_MODULE_PATH "@PACKAGE_MODULE_INSTALL_DIR@")
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${ParELAG_MODULE_PATH}")

find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)

if (NOT METIS_DIR)
  set(METIS_DIR "@METIS_DIR@")
endif()
find_package(METIS REQUIRED)

if (NOT SuiteSparse_DIR)
  set(SuiteSparse_DIR "@SuiteSparse_DIR@")
endif()
find_package(SuiteSparse REQUIRED)

if (NOT HYPRE_DIR)
  set(HYPRE_DIR "@HYPRE_DIR@")
endif()
if (NOT MFEM_DIR)
  set(MFEM_DIR "@MFEM_DIR@")
endif()
find_package(MFEM REQUIRED)

# Set the MPI_CXX_COMPILER to help FindMPI out
set(MPI_CXX_COMPILER "@MPI_CXX_COMPILER@")
find_dependency(MPI)

if (MPI_CXX_FOUND)
  if (NOT TARGET MPI::MPI_CXX)
    add_library(MPI::MPI_CXX INTERFACE IMPORTED)

    if (MPI_CXX_COMPILE_FLAGS)
      separate_arguments(_MPI_CXX_COMPILE_OPTIONS UNIX_COMMAND
        "${MPI_CXX_COMPILE_FLAGS}")
      set_property(TARGET MPI::MPI_CXX PROPERTY
        INTERFACE_COMPILE_OPTIONS "${_MPI_CXX_COMPILE_OPTIONS}")
    endif()

    if(MPI_CXX_LINK_FLAGS)
      separate_arguments(_MPI_CXX_LINK_LINE UNIX_COMMAND
        "${MPI_CXX_LINK_FLAGS}")
    endif()
    list(APPEND _MPI_CXX_LINK_LINE "${MPI_CXX_LIBRARIES}")

    set_property(TARGET MPI::MPI_CXX PROPERTY
      INTERFACE_LINK_LIBRARIES "${_MPI_CXX_LINK_LINE}")

    set_property(TARGET MPI::MPI_CXX PROPERTY
      INTERFACE_INCLUDE_DIRECTORIES "${MPI_CXX_INCLUDE_PATH}")

  endif (NOT TARGET MPI::MPI_CXX)
else (MPI_CXX_FOUND)

  message(FATAL_ERROR "MPI not found for CXX language.")
  
endif (MPI_CXX_FOUND)

#
# OPTIONAL DEPENDENCIES
#

if (ParELAG_USE_MARS)
  if (NOT MARS_DIR)
    set(MARS_DIR "@MARS_DIR@")
  endif()
  find_package(MARS REQUIRED)
endif()

# OpenMP
if (ParELAG_ENABLE_OPENMP)
  find_package(OpenMP REQUIRED)

  list(APPEND ParELAG_C_FLAGS "${OpenMP_C_FLAGS}")
  list(APPEND ParELAG_CXX_FLAGS "${OpenMP_CXX_FLAGS}")
endif()

if (ParELAG_ENABLE_PARMETIS)
  if (NOT ParMETIS_DIR)
    set(ParMETIS_DIR "@ParMETIS_DIR@")
  endif()
  find_package(ParMETIS REQUIRED)
endif()


# SuperLU
if (ParELAG_ENABLE_SUPERLU)
  if (NOT SuperLU_DIR)
    set(SuperLU_DIR "@SuperLU_DIR@")
  endif()
  find_package(SuperLU REQUIRED)
endif()

# SuperLU_DIST
if (ParELAG_ENABLE_SUPERLUDIST)
  if (NOT SuperLUDist_DIR)
    set(SuperLUDist_DIR "@SuperLUDist_DIR@")
  endif()
  find_package(SuperLUDist REQUIRED)
endif()


# strumpack
if (ParELAG_ENABLE_STRUMPACK)
  if (NOT ParMETIS_DIR)
    set(ParMETIS_DIR "@ParMETIS_DIR@")
  endif()
  if (NOT SCOTCH_DIR)
    set(SCOTCH_DIR "@SCOTCH_DIR@")
  endif()
  if (NOT ScaLAPACK_DIR_DIR)
    set(ScaLAPACK_DIR_DIR "@ScaLAPACK_DIR_DIR@")
  endif()
  if (NOT STRUMPACK_DIR)
    set(STRUMPACK_DIR "@STRUMPACK_DIR@")
  endif()
  
  find_package(Strumpack REQUIRED)
endif()

# petsc
if (ParELAG_ENABLE_PETSC)
  if (NOT PETSC_DIR)
    set(PETSC_DIR "@PETSC_DIR@")
  endif()
  find_package(PETSc REQUIRED)
endif()

set(ParELAG_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@")
foreach (dir ${ParELAG_INCLUDE_DIRS})
  set_and_check(ParELAG_INCLUDE_DIRS "${dir}")
endforeach (dir ${ParELAG_INCLUDE_DIRS})

set_and_check(ParELAG_LIBRARY_DIR "@PACKAGE_LIB_INSTALL_DIR@")
check_required_components(ParELAG)

if (NOT TARGET parelag::ParELAG)
  include(${CMAKE_CURRENT_LIST_DIR}/ParELAGTargets.cmake)
endif (NOT TARGET parelag::ParELAG)

set(ParELAG_LIBRARIES parelag::ParELAG)
